<?php
/**
 * Created by IntelliJ IDEA.
 * User: Maxwell
 */

namespace Home\Org\Util;
use Home\Org\Util\Sqlserver;

class SQLSERVERDATASINGLETON
{
    /**
     * 将一些结构,内容不经常变化的数据用单例缓存, 以减轻数据库压力(主要应用场景: EXCEL导入)
     */

    static private $_instance = null;
    public $_dataTI_Z0151 = null;

    private function __construct()
    {
        // 仅执行一次
        $this->_dataTI_Z0151 = $this->getitemcategorycode();

    }

    /**
     * @return array|null
     */
    public function getDataTIZ0151()
    {
        return $this->_dataTI_Z0151;
    }

    static public function getInstance()
    {
        if (is_null(self::$_instance)) {
            self::$_instance = new SQLSERVERDATASINGLETON();
        }
        return self::$_instance;
    }

    private function getitemcategorycode(){

        import("@.ORG.Util.Sqlserver");
        $sqlObj  = new Sqlserver();
        $where = '';
//      $key = iconv('utf-8', 'GBK', $key);
        $sql = "select U_nodecode,U_fahercode,U_nodename,U_is3C from [@TI_Z0151]";
        $res = $sqlObj->query($sql);
        $tree = array();
        foreach ($res as $key => $value)
        {
            // $key : 0, 1, 2, 3,
            // $value : array with key
            if ($value['U_fahercode']){
                $tree[] = array ('id'=>$value['U_nodecode'],'text'=>$value['U_nodename'],'pid'=>$value['U_fahercode'],'is3C'=>$value['U_is3C']);
            }else{
                $tree[] = array ('id'=>$value['U_nodecode'],'text'=>$value['U_nodename'],'checked'=>'true','is3C'=>$value['U_is3C']);
            }
        }
        return $tree;
        //echo json_encode($tree);

    }



}